package com.luojie.service.impl;


import com.luojie.dto.MatchesDTO;
import com.luojie.dto.MatchesQueryDTO;
import com.luojie.mapper.BallMapper;
import com.luojie.mapper.MatchesMapper;
import com.luojie.po.Ball;
import com.luojie.po.Matches;
import com.luojie.service.MatchesService;
import com.luojie.vo.BallVo;
import com.luojie.vo.MatchesVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class MatchesServiceImpl implements MatchesService {
    @Autowired
    private MatchesMapper matchesMapper;
    @Autowired
    private BallMapper ballMapper;

    @Override
    public void addMatch(MatchesDTO matchesDTO) throws RuntimeException {
        //新增比赛
        if (ObjectUtils.isEmpty(matchesDTO)) {
            throw new RuntimeException("参数错误");
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime registrationTime = LocalDateTime.parse(matchesDTO.getRegistrationTime(), formatter);
        LocalDateTime registrationEndTime = LocalDateTime.parse(matchesDTO.getRegistrationEndTime(), formatter);
        LocalDateTime  startTime = LocalDateTime.parse(matchesDTO.getStartTime(), formatter);

        // 校验报名开始时间不能晚于报名结束时间
        if (registrationTime.isAfter(registrationEndTime)) {
            throw new RuntimeException("开始时间不能晚于报名截止时间");
        }
        // 校验报名结束时间不能晚于比赛开始时间
        if (startTime.isBefore(registrationTime)) {
            throw new RuntimeException("比赛开始时间不能早于报名开始时间");
        }
        // 校验报名截止时间不能晚于比赛开始时间
        if (startTime.isBefore(registrationEndTime)) {
            throw new RuntimeException("报名结束时间不能晚于比赛开始时间");
        }

        Matches matches = new Matches();
        matches.setAddress(matchesDTO.getAddress());
        matches.setName(matchesDTO.getName());
        matches.setPayMode(matchesDTO.getPayMode());
        matches.setUserName(matchesDTO.getUserName());
        matches.setPhone(matchesDTO.getPhone());
        matches.setTopImage(matchesDTO.getTopImage());
        matches.setRegistrationTime(registrationTime);
        matches.setRegistrationEndTime(registrationEndTime);
        matches.setStartTime(startTime);

        LocalDateTime now = LocalDateTime.now();
        matches.setCreateTime(now);
        matches.setUpdateTime(now);
        //如果今天在开始时间和结束时间之类把状态设置成
        if (matches.getRegistrationTime().isBefore(now) && matches.getRegistrationEndTime().isAfter(now)) {
            matches.setStatus(1);
            matchesMapper.insert(matches);
        } else {
            matches.setStatus(0);
            matchesMapper.insert(matches);
        }

    }

    @Override
    public MatchesVo selectMatches(Integer id) {
        MatchesVo matchesVo = new MatchesVo();
        if (id == null) {
            return matchesVo;
        }
        Matches matches = matchesMapper.selectById(id);
        if (ObjectUtils.isEmpty(matches)) {
            return matchesVo;
        }
        List<Ball> ballList = ballMapper.selectByMachesId(matches.getId());
        matchesVo.setName(matches.getName());
        matchesVo.setAddress(matches.getAddress());
        matchesVo.setUserName(matches.getUserName());
        matchesVo.setStartTime(matches.getStartTime().toString());
        if (CollectionUtils.isEmpty(ballList)) {
            return matchesVo;
        }
        List<BallVo> ballVos = new ArrayList<>();
        for (Ball ball : ballList) {
            BallVo ballVo = new BallVo();
            ballVo.setBallGamesName(ball.getBallGamesName());
            ballVo.setCompetitionFormat(ball.getCompetitionFormat());
            ballVo.setKickOffTime(ball.getKickOffTime().toString());
            ballVo.setNumberParticipants(ball.getNumberParticipants());
            ballVo.setRestriction(ball.getRestriction());
            ballVo.setRegistrationFee(ball.getRegistrationFee());
            ballVo.setTotalNumberParticipants(ball.getTotalNumberParticipants());
            ballVo.setBallGames(ball.getBallGames());
            ballVos.add(ballVo);
        }
        matchesVo.setProject(ballVos);
        return matchesVo;
    }

    @Override
    @Transactional
    public void deletById(Integer id) {
        matchesMapper.deletById(id);
        ballMapper.deletById(id);
    }

    @Override
    public void updateMatch(MatchesDTO matchesDTO) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime registrationTime = LocalDateTime.parse(matchesDTO.getRegistrationTime(), formatter);
        LocalDateTime registrationEndTime = LocalDateTime.parse(matchesDTO.getRegistrationEndTime(), formatter);
        LocalDateTime  startTime = LocalDateTime.parse(matchesDTO.getStartTime(), formatter);
        // 校验报名开始时间不能晚于报名结束时间
        if (registrationTime.isAfter(registrationEndTime)) {
            throw new RuntimeException("开始时间不能晚于报名截止时间");
        }
        // 校验报名结束时间不能晚于比赛开始时间
        if (startTime.isBefore(registrationTime)) {
            throw new RuntimeException("比赛开始时间不能早于报名开始时间");
        }
        // 校验报名截止时间不能晚于比赛开始时间
        if (startTime.isBefore(registrationEndTime)) {
            throw new RuntimeException("报名结束时间不能晚于比赛开始时间");
        }
        LocalDateTime now = LocalDateTime.now();
        Matches matches = new Matches();
        matches.setAddress(matchesDTO.getAddress());
        matches.setName(matchesDTO.getName());
        matches.setPayMode(matchesDTO.getPayMode());
        matches.setUserName(matchesDTO.getUserName());
        matches.setPhone(matchesDTO.getPhone());
        matches.setTopImage(matchesDTO.getTopImage());
        matches.setRegistrationTime(registrationTime);
        matches.setRegistrationEndTime(registrationEndTime);
        matches.setStartTime(startTime);
        matches.setUpdateTime(now);
        if (registrationTime.isBefore(now) && registrationEndTime.isAfter(now)) {
            matches.setStatus(1);
            matchesMapper.updateMatch(matches,matchesDTO.getId());
        } else {
           matches.setStatus(0);
            matchesMapper.updateMatch(matches,matchesDTO.getId());
        }
    }

    @Override
    public List<MatchesVo> page(MatchesQueryDTO matchesDTO) {
        if (ObjectUtils.isEmpty(matchesDTO)) {
            List<Matches> matchesList = matchesMapper.selects();
            if (CollectionUtils.isEmpty(matchesList)) {
                return new ArrayList<>();
            }
            List<Integer> ids = matchesList.stream().map(Matches::getId).collect(Collectors.toList());
            List<Ball> ballList = ballMapper.selectByMachesIds(ids);
            List<MatchesVo> matchesVos = new ArrayList<>();
            for (Matches matches : matchesList) {
                MatchesVo matchesVo = new MatchesVo();
                matchesVo.setName(matches.getName());
                matchesVo.setAddress(matches.getAddress());
                matchesVo.setStartTime(matches.getStartTime().toString());
                if (CollectionUtils.isEmpty(ballList)) {
                    return new ArrayList<>();
                }
                List<BallVo> ballVos = new ArrayList<>();
                for (Ball ball : ballList) {
                    BallVo ballVo = new BallVo();
                    ballVo.setStatus(ball.getStatus());
                    ballVo.setBallGamesName(ball.getBallGamesName());
                    ballVo.setTotalNumberParticipants(ball.getTotalNumberParticipants());
                    ballVo.setNumberParticipants(ball.getNumberParticipants());
                    ballVos.add(ballVo);
                    matchesVo.setProject(ballVos);
                }
                matchesVos.add(matchesVo);
            }
            return matchesVos;

        } else {

            String address = matchesDTO.getAddress();
            String name = matchesDTO.getName();
            LocalDateTime startTime = matchesDTO.getStartTime();
            LocalDateTime endTime = matchesDTO.getEndTime();
            List<Matches> matchesList = matchesMapper.page(address, name, startTime, endTime);
            if (CollectionUtils.isEmpty(matchesList)) {
                return new ArrayList<>();
            }
            List<Integer> ids = matchesList.stream().map(Matches::getId).collect(Collectors.toList());
            List<Ball> ballList = ballMapper.selectByMachesIds(ids);
            List<MatchesVo> matchesVos = new ArrayList<>();

            for (Matches matches : matchesList) {
                MatchesVo matchesVo = new MatchesVo();
                matchesVo.setName(matches.getName());
                matchesVo.setAddress(matches.getAddress());
                matchesVo.setStartTime(matches.getStartTime().toString());
                if (CollectionUtils.isEmpty(ballList)) {
                    return new ArrayList<>();
                }
                List<BallVo> ballVos = new ArrayList<>();

                for (Ball ball : ballList) {
                    if (ball.getMatchesId().equals(matches.getId())) {
                        BallVo ballVo = new BallVo();
                        ballVo.setStatus(ball.getStatus());
                        ballVo.setBallGamesName(ball.getBallGamesName());
                        ballVo.setTotalNumberParticipants(ball.getTotalNumberParticipants());
                        ballVo.setNumberParticipants(ball.getNumberParticipants());
                        ballVos.add(ballVo);
                    }
                }
                matchesVo.setProject(ballVos);
                matchesVos.add(matchesVo);
            }
            return matchesVos;
        }
    }

}
